package com.example.androidlesson;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.androidlesson.adapters.NewCustomerItemAdapter;
import com.example.androidlesson.api.ApiService;
import com.example.androidlesson.interfaces.IItemDataBind;
import com.example.androidlesson.model.FruitItemEntity;
import com.google.android.material.datepicker.MaterialDatePicker;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


public class FruitFragment extends Fragment {

    private static final String BaseURL = "http://47.97.204.181:9105/";
    private static FruitFragment instance;
    private NewCustomerItemAdapter adapter=null;
    private ListView listView;
    private FruitFragment() {}
    public static FruitFragment  GetFruitFragment(){
        if(instance==null){
            instance = new FruitFragment();
        }
        return instance;
    }

    private IItemDataBind ListBinder=new IItemDataBind() {

        @Override
        public int getViewTypeCount() {
            return 0;
        }

        @Override
        public int getItemViewType(Object O) {
            return 0;
        }

        @Override
        public View GetItemView(Object O, Context con) {
            View  V=null;
            if (O instanceof FruitItemEntity) {
                V=LayoutInflater.from(con).inflate(R.layout.frui_listitem, null);

                TextView fruitname=(TextView) V.findViewById(R.id.fruitname);
                if(fruitname!=null)
                {
                    //  fruitname.d
                    //fruitname.setOnContextClickListener();
                }
            }
            return V;
        }
        @Override
        public View GetItemDropdownItemView(Object O, Context con) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void Binder(Object O, int position, View V, View dpv) {
            // TODO Auto-generated method stub
            if ((O!=null) && (V!=null)) {
                if (O instanceof  FruitItemEntity) {
                    FruitItemEntity  item=(FruitItemEntity)O;
                    TextView tvname=(TextView) V.findViewById(R.id.fruitname);
                    tvname.setText(item.getFruitname());

                    TextView tvpinci=(TextView) V.findViewById(R.id.introduce);
                    tvpinci.setText(item.getIntroduce());


                }

                if ((position % 2)==0) {
                    V.setBackgroundColor(0xffeeeeee);
                }else {
                    V.setBackgroundColor(0xffffffff);
                }

            }
        }
    };

    private void getFruitHttp() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BaseURL)
                .build();

        retrofit.create(ApiService.class)
                .getFruitList("1", "20")
                .enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        if (response.isSuccessful()) {
                            try {
                                String string = response.body().string();

                                JSONObject jobj= new JSONObject(string);
                                String  msg=jobj.getString("msg");
                                JSONArray listdata= jobj.getJSONArray("resultData") ;
                                if(listdata.length()>0) {
                                    for(int k=0;k<listdata.length();k++)
                                    {
                                        JSONObject model=  listdata.getJSONObject(k);
                                        String fruitName=model.getString("FruitName");
                                        String introduce=model.getString("Introduce");
                                        FruitItemEntity item = new FruitItemEntity(fruitName, "", "", "", introduce);
                                        adapter.Add(item);
                                    }
                                    // adpter.notifyDataSetChanged(); //通知数据
                                }
                                Log.e("xyh", "onResponse: " + string);
                            } catch (IOException | JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        Log.e("xyh", "onFailure: " + t.getMessage());
                    }
                });
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fruit,container,false);
        listView=(ListView) view.findViewById(R.id.listView1);
        if(listView!=null){
            listView.setDividerHeight(0);
            adapter = new NewCustomerItemAdapter(this.getActivity());
            adapter.SetBinder(ListBinder);
            listView.setAdapter(adapter);
            listView.setSelector(new ColorDrawable(Color.TRANSPARENT));
            AbsListView.OnScrollListener listener=new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    if (AbsListView.OnScrollListener.SCROLL_STATE_IDLE==scrollState){
                        // ??
                    }else{
                        //isScrollIdle=false;
                    }
                }
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    if ((firstVisibleItem+visibleItemCount)==totalItemCount){
                        // ??
                    }else{
                        // isScrollIdle=false;
                    }
                }
            };
            listView.setOnScrollListener(listener);
            getFruitHttp();
        }
        return view;
    }
}